package com.datastructure.test.pruneleaves;

public class PruneLeaves {
    public static void main(String[] args) {
        PruneLeaves pruneLeaves = new PruneLeaves();
        TreeNode root = new TreeNode(1);
        root.left=new TreeNode(1);
        root.right=new TreeNode(1);
        root.left.left=new TreeNode(1);
        root.left.right=new TreeNode(1);
        root.right.left=new TreeNode(1);
        root.right.right=new TreeNode(1);
        root=pruneLeaves.pruneLeaves(root);
        System.out.println(root);
    }

    public TreeNode pruneLeaves (TreeNode root) {
        if(root == null)
            return null;
        if(root.left != null && isLeaf(root.left))  //判断该节点左节点是否为叶节点，如果是，该节点需要删除
            return null;
        if(root.right != null && isLeaf(root.right))  //判断该节点右节点是否为叶节点，如果是，该节点需要删除
            return null;
        root.left = pruneLeaves(root.left);
        root.right = pruneLeaves(root.right);
        return root;
    }
    public boolean isLeaf(TreeNode node){
        return node!=null && (node.left == null && node.right == null );
    }
}
class TreeNode {
   int val = 0;
   TreeNode left = null;
   TreeNode right = null;
   public TreeNode(int val) {
     this.val = val;
   }
 }
